<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\GoodsModel;
use think\Controller;
//goods_type 1是产品2时候标的
class Goods extends Controller{


	//产品大纲显示
	public function product(){

		$goods = db('goods');
		if(request()->isAjax()){
            $return = $goods -> where("goods_type = 1") -> select();
			if($return){
			 return json($return);
			}
		}else{
		// 渲染模板输出
		return $this->fetch();
		}
	
	}
	//热销产品大纲显示
	public function hotproduct(){

		$goods = db('goods');
		if(request()->isAjax()){
            $return = $goods -> where("goods_type = 3") -> select();
			if($return){
			 return json($return);
			}
		}else{
		// 渲染模板输出
		return $this->fetch();
		}
	
	}
	// 新增热销产品
	public function hotproductAdd(){

		$fuzzy = db('select_cat');	
		$arr = $fuzzy ->select();
		$s = $this ->getTrees($arr,0);
		$list = db('goods_cat')->where('parent_id',1)->select();

		if(request()->isPost()){

			$file = request()->file("pic");
			$route = '';
			if($file){
				// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		    }

		    	$data = input('post.');

		        $data["pic"] = $route;

		        $result = db('goods_cat')->where('cat_id',$data["cat_id"])-> select();
		        $data["type"] = $result[0]["cat_name"];


		        $result = db('goods')->insert($data);  
		        if($result){
		        	$this->assign('list',$list);
		        	$this->success('新增成功', 'hotproductAdd','',2);
		        }
			
		}else{
		$this->assign([
            'list'  => $list,
            'cat' => $s
        ]);
		

		return $this->fetch('hotproductAdd');
	}
 }
	// 新增大纲产品
	public function productAdd(){

		$fuzzy = db('select_cat');	
		$arr = $fuzzy ->select();
		$s = $this ->getTrees($arr,0);
		$list = db('goods_cat')->where('parent_id',1)->select();

		if(request()->isPost()){

			$file = request()->file("pic");
			$route = '';
			if($file){
				// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        
		        //echo db('goods')->getLastSql();
		        // var_dump($data);
		    }

		    	$data = input('post.');
		    	// if($data['stair'] ==0 or $data['second_level'] ==0 or $data['three_level'] ==0 ){
		    	// 	$this->success('请填写完产品分类', 'productAdd','',3);
		    	// }
		    	

		    	// if(!array_key_exists('content',$data)){
		    	// 	$this->success('请填写产品详情', 'productAdd','',3);
		    	// }
		    	
		        $data["pic"] = $route;

		        $result = db('goods_cat')->where('cat_id',$data["cat_id"])-> select();
		        $data["type"] = $result[0]["cat_name"];


		        $result = db('goods')->insert($data);  
		        if($result){
		        	$this->assign('list',$list);
		        	$this->success('新增成功', 'productAdd','',2);
		        }
			
		}else{
		$this->assign([
            'list'  => $list,
            'cat' => $s
        ]);
		

		return $this->fetch('productAdd');
	}
 }
	// 删除大纲产品
	public function productRemove(){
		// 接受 json 字符串
		$requestDataJson = $GLOBALS['HTTP_RAW_POST_DATA'];
		
		// json 字符串反序列化成数组
		$requestData = json_decode($requestDataJson,true);

		$result = db('goods')->where('id')->delete($requestData["id"]);

		if($result){
			echo 1;
		}
	}
	// 更改热销产品
	public function hotproductChange(){

		// 下拉框
		$list = db('goods_cat')->where('parent_id',1)->select();
		$this->assign('list',$list);

		// 接受id 返回 商品详情
		if(request()->isGet()){
			$data = input('get.id');
			// 产品详情
			$formData = db('goods')->where('id',$data)->select();

			$this->assign('formData',$formData);
		}

		// 跟新数据
		if(request()->isPost()){

			$file = request()->file("pic");
			if($file){
				// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $data = input('post.');
		        $data["pic"] = $route;
		          $arr = db('goods_cat')->where('cat_id',$data["cat_id"]) ->find();
				$data["type"] = $arr["cat_name"] ;
		        $id = $data["id"];
		        $result = db('goods')->where('id',$id)->update($data);

		        if($result){
		        	$this->assign('list',$list);
		        	$this->success('更改成功', 'hotproduct','',2);
		        }	
			}else {

				$data = input('post.');
				 $arr = db('goods_cat')->where('cat_id',$data["cat_id"]) ->find();
				$data["type"] = $arr["cat_name"] ;
				$id = $data["id"];
				$result = db('goods')->where('id',$id)->update($data);
				if($result){
		        	$this->assign('list',$list);
		        	$this->success('更改成功', 'hotproduct','',2);
		        }	
			}
		}else{	

		// // 跟新数据
		// if(request()->isPost()){
		// 	$data = input('post.');
		// 	$id = $data["id"];
		// 	$result = db('goods')->where('id',$id)->update($data);
			
		// 	if($result){

		// 		$this->assign('list',$list);
		// 		// return $this->fetch('productAdd');
		// 		$this->success('更改成功', 'product','',2);
		// 	}
		// }

		return $this->fetch('hotproductChange');}
	}
	// 更改大纲产品
	public function productChange(){

		// 下拉框
		$list = db('goods_cat')->where('parent_id',1)->select();
		$this->assign('list',$list);

		// 接受id 返回 商品详情
		if(request()->isGet()){
			$data = input('get.id');
			// 产品详情
			$formData = db('goods')->where('id',$data)->select();

			$this->assign('formData',$formData);
		}

		// 跟新数据
		if(request()->isPost()){

			$file = request()->file("pic");
			if($file){
				// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $data = input('post.');
		        $data["pic"] = $route;
		          $arr = db('goods_cat')->where('cat_id',$data["cat_id"]) ->find();
				$data["type"] = $arr["cat_name"] ;
		        $id = $data["id"];
		        $result = db('goods')->where('id',$id)->update($data);

		        if($result){
		        	$this->assign('list',$list);
		        	$this->success('更改成功', 'product','',2);
		        }	
			}else {

				$data = input('post.');
				 $arr = db('goods_cat')->where('cat_id',$data["cat_id"]) ->find();
				$data["type"] = $arr["cat_name"] ;
				$id = $data["id"];
				$result = db('goods')->where('id',$id)->update($data);
				if($result){
		        	$this->assign('list',$list);
		        	$this->success('更改成功', 'product','',2);
		        }	
			}
		}else{	

		// // 跟新数据
		// if(request()->isPost()){
		// 	$data = input('post.');
		// 	$id = $data["id"];
		// 	$result = db('goods')->where('id',$id)->update($data);
			
		// 	if($result){

		// 		$this->assign('list',$list);
		// 		// return $this->fetch('productAdd');
		// 		$this->success('更改成功', 'product','',2);
		// 	}
		// }

		return $this->fetch('productChange');}
	}

	// 查看详情大纲产品
	public function productCheck(){
		// 接受id 返回 商品详情
		if(request()->isGet()){
			$data = input('get.id');
			// 产品详情
			$formData = db('goods')->where('id',$data)->select();
			$cat =db('select_cat'); 
			foreach ($formData  as $key => $value) {
			           	
			           $arr	= $cat ->where('id',$value['stair']) ->find();
			           $formData[$key]['stair'] = $arr['name'];
			           $arr	= $cat ->where('id',$value['three_level']) ->find();
			           $formData[$key]['three_level'] = $arr['name'];
			           $arr	= $cat ->where('id',$value['second_level']) ->find();
			           $formData[$key]['second_level'] = $arr['name'];
			           }           
			$this->assign('formData',$formData);
			return $this->fetch('productCheck');
		}else {
			$this->error('查看失败');
		}
	}

	// 产品分类列表	
	public function productCat(){
		$goods = db('goods_cat');
		if(request()->isAjax()){

            $param = input();
          	$return = $goods -> where("parent_id = '1'") -> select();
          	$arr ;

          	foreach ($return as $key => $value) {
          		$value['control'] = $return[$key]["cat_id"];
          		$arr[$key] = $value;
          	}

            return json($arr);

        }

      return $this->fetch();
	}

	// 添加产品大纲分类
	public function productCatAdd(){
		$cat_name = $_POST['classfiyName'];
		$arry = array('cat_name' => $cat_name ,'parent_id' =>1 );
		$goods = db('goods_cat');
		$id = $goods ->insert($arry);
		if($id){
			$return = $goods -> where("parent_id = '1'") -> select();
          	$arr ;

          	foreach ($return as $key => $value) {
          		$value['control'] = $return[$key]["cat_id"];
          		$arr[$key] = $value;
          	}

            return json($arr);
		}
		
	}

	// 删除产品大纲分类
	public function productCatRemove(){
		// 接受 json 字符串
		$requestDataJson = $GLOBALS['HTTP_RAW_POST_DATA'];
		
		// json 字符串反序列化成数组
		$requestData = json_decode($requestDataJson,true);

		// 数组,可能多 id 
		$removeIds = $requestData["classifyId"];

		// 判断是否数组 , 选择右边删除为字符串 , 左边多选为数组
		$isArray = is_array($removeIds);

		if($isArray){
			// 选择多选
			foreach ($removeIds as $key => $value) {
	      		$removeProdct = db('goods')->where('cat_id',$value)->delete();
	     
	      			$result = db('goods_cat')->where('cat_id')->delete($removeIds);
	   
	      	};
		}else {
			// 选择单选
			$removeProdct = db('goods')->where('cat_id',$removeIds)->delete();
			if($removeProdct){
      			$result = db('goods_cat')->where('cat_id')->delete($removeIds);
      		}
		}

		// 成功
		if($result){
			echo 1;
		}
	}

	// 更新产品大纲分类
	public function productCatUpdata(){
		$requestData = input('post.classifyId');
		$conten = input('post.content');
		$result = db('goods_cat')->where('cat_id',$requestData)->setField('cat_name',$conten);

		if($result){
			echo 1;
		}
	}


	// 标的商品显示与添加
	public function matter(){

		$goods = db('goods');
		 if(request()->isAjax()){
            $return = $goods -> where("goods_type = 2") -> select();
			if($return){
			 return json($return);
			}
		}else{
		// 渲染模板输出
		return $this->fetch();
		}
	}

	// 标的商品添加
	public function matterAdd(){
		$list = db('goods_cat')->where('parent_id',2)->select();
		$this->assign('list',$list);

		if(request()->isPost()){
			$route = '';
			$file = request()->file("pic");

			if($file){
				// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		       
			}
			 	$data = input('post.');
			 	//  if($data['stair'] ==0 or $data['second_level'] ==0 or $data['three_level'] ==0 ){
		   //  		$this->success('请填写完产品分类', 'productAdd','',3);
		   //  	}
		    	

		   //  	if(!array_key_exists('content',$data)){
		   //  		$this->success('请填写产品详情', 'productAdd','',3);
		   //  	}
		        $data["pic"] = $route;

		        $result = db('goods_cat')->where('cat_id',$data["cat_id"])-> select();
		        $data["type"] = $result[0]["cat_name"];

		        
		        $result = db('goods')->insert($data);
		        if($result){
		        	$this->assign('list',$list);
		        	$this->success('新增成功', 'matter','',2);
		        }
			
		}else{

		

		return $this->fetch('matterAdd');
	}
 }
	public function matterRemove(){
		// 接受 json 字符串
		$requestDataJson = $GLOBALS['HTTP_RAW_POST_DATA'];
		
		// json 字符串反序列化成数组
		$requestData = json_decode($requestDataJson,true);

		$result = db('goods')->where('id')->delete($requestData["id"]);

		if($result){
			echo 1;
		}
	}

	public function matterChange(){
		// 下拉框
		$list = db('goods_cat')->where('parent_id',2)->select();
		$this->assign('list',$list);

		// 接受id 返回 商品详情
		if(request()->isGet()){
			$data = input('get.id');
			// 产品详情
			$formData = db('goods')->where('id',$data)->select();
			$cat =db('select_cat'); 
			foreach ($formData  as $key => $value) {
			           	
			           $arr	= $cat ->where('id',$value['stair']) ->find();
			           $formData[$key]['stair'] = $arr['name'];
			           $arr	= $cat ->where('id',$value['three_level']) ->find();
			           $formData[$key]['three_level'] = $arr['name'];
			           $arr	= $cat ->where('id',$value['second_level']) ->find();
			           $formData[$key]['second_level'] = $arr['name'];
			           }    

			$this->assign('formData',$formData);
		}

		// 跟新数据
		if(request()->isPost()){
			$file = request()->file("pic");
			if($file){
				// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $data = input('post.');
		        $data["pic"] = $route;
		       
		        //unset($data[''])
		  //       $arr = db('goods_cat')->where('cat_id',$data["cat_id"]) ->find();
				// $data["type"] = $arr["cat_name"] ;
		        $id = $data["id"];
		        //var_dump($data);die();
		        $result = db('goods')->where('id',$id)->update($data);

		        if($result){
		        	$this->assign('list',$list);
		        	$this->success('更改成功', 'matter','',2);
		        }	
			}else {
				$data = input('post.');
				$id = $data["id"];

				// $arr = db('goods_cat')->where('cat_id',$data["cat_id"]) ->find();
				// $data["type"] = $arr["cat_name"] ;

				 //var_dump($data);die();
				$result = db('goods')->where('id',$id)->update($data);
				if($result){
		        	$this->assign('list',$list);
		        	$this->success('更改成功', 'matter','',2);
		        }	
			}
		}else{

		return $this->fetch('matterChange');
	}
}
	public function matterCheck(){
		// 接受id 返回 商品详情
		if(request()->isGet()){
			$data = input('get.id');
			// 产品详情
			$formData = db('goods')->where('id',$data)->select();
			$cat =db('select_cat'); 
			foreach ($formData  as $key => $value) {
			           	
			           $arr	= $cat ->where('id',$value['stair']) ->find();
			           $formData[$key]['stair'] = $arr['name'];
			           $arr	= $cat ->where('id',$value['three_level']) ->find();
			           $formData[$key]['three_level'] = $arr['name'];
			           $arr	= $cat ->where('id',$value['second_level']) ->find();
			           $formData[$key]['second_level'] = $arr['name'];
			           }    

			$this->assign('formData',$formData);
			return $this->fetch('matterCheck');
		}else {
			$this->error('查看失败');
		}
	}

	// 标的分类页面
	public function matterCat(){
		$goods = db('goods_cat');
		if(request()->isAjax()){

            $param = input();
          	$return = $goods -> where("parent_id = '2'") -> select();
          	$arr ;

          	foreach ($return as $key => $value) {
          		$value['control'] = $return[$key]["cat_id"];
          		$arr[$key] = $value;
          	}

            return json($arr);

        }

      return $this->fetch('matterCat');
	}

	// 添加标的分类
	public function matterCatAdd(){

		if(request()->isPost()){
			$file = request()->file("cat_pic");
			if($file){
    			// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $data = input('post.');
		        $data["cat_pic"] = $route;

		        $result = db('goods_cat')->insert($data);

		        if($result){
		        	$this->success('新增成功', 'matterCat','',2);
		        }

    		}
		}

		return $this->fetch('matterCatAdd');
		
	}

	// 删除标的分类
	public function matterCatRemove(){

		// 接受 json 字符串
		$requestDataJson = $GLOBALS['HTTP_RAW_POST_DATA'];
		
		// json 字符串反序列化成数组
		$requestData = json_decode($requestDataJson,true);

		// 数组,可能多 id 
		$removeIds = $requestData["classifyId"];

		// 判断是否数组 , 选择右边删除为字符串 , 左边多选为数组
		$isArray = is_array($removeIds);

		if($isArray){
			// 选择多选
			foreach ($removeIds as $key => $value) {
	      		$removeProdct = db('goods')->where('cat_id',$value)->delete();
	      		$result = db('goods_cat')->where('cat_id')->delete($removeIds);
	      		// if($removeProdct){
	      		// 	$result = db('goods_cat')->where('cat_id')->delete($removeIds);
	      		// }
	      	};
		}else {
			// 选择单选 删除旗下商品
			$removeProdct = db('goods')->where('cat_id',$removeIds)->delete();
			// 再删除分类
      		$result = db('goods_cat')->where('cat_id')->delete($removeIds);
		}

		// 成功
		if($result){
			echo 1;
		}
	}

	// 更新产品大纲分类
	public function matterCatUpdata(){
		$id = input('get.id');

		$formData = db('goods_cat')->where('cat_id',$id)->select();

		$this->assign('formData',$formData);


		// 如果修改了数据
		if(request()->isPost()){
		// 获取表单上传文件 例如上传了001.jpg
    		$file = request()->file("cat_pic");

    		// 如果有上传图片
    		if($file){
    			// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $upData = input('post.');
		        $upData["cat_pic"] = $route;

		        $result = db('goods_cat')->where('cat_id',$upData["cat_id"])->update($upData);

		        if($result){
		        	$this->success('修改成功', 'matterCat','',2);
		        }

    		}else {

    			$upData = input('post.');
    			$result = db('goods_cat')->where('cat_id',$upData["cat_id"])->update($upData);
    			if($result){
		        	$this->success('修改成功', 'matterCat','',2);
		        }
    		}
    		
		}

		return $this->fetch('matterCatUpdata');

		// $conten = input('post.content');
		// $result = db('goods_cat')->where('cat_id',$requestData)->setField('cat_name',$conten);

		// if($result){
		// 	echo 1;
		// }
	}
	function getTrees($data, $pId)
		{
		$tree = '';
		foreach($data as $k => $v)
		{
		  if($v['parent_id'] == $pId)
		  {        //父亲找到儿子
		   $v['parent_id'] = $this ->getTrees($data, $v['id']);
		   $tree[] = $v;
		   //unset($data[$k]);
		  }
		}
		return $tree;
		}
		//模糊查询分类
		public function fuzzyCat(){
			$fuzzy = db('select_cat');
			//第一步传分类的树形图给前端
			if(request()->isAjax()){
				$arr = $fuzzy ->select();

				//转出树形图
				$s = $this ->getTrees($arr,0);
				if($_POST !=null){
					$arra  = array('parent_id' => $_POST['id'],
									'name' => $_POST['check']
													 );
					$id = $fuzzy ->insert($arra);
					if($id){
						return 1;
					}
				}
				return json($s);
				
				}
				//接收前端返回的数据保存数据库
				// $cat = input();
				// $id = $fuzzy ->insert($cat);

			return $this->fetch();
		}

		public function fuzzyList(){
			$fuzzy = db('select_cat');
			
				$arr = $fuzzy ->select();
				// //转出树形图
				if(request()->isAjax()){
				$s = $this ->getTrees($arr,0);
				return json($arr);
				}
				
			
			return $this->fetch('addclassify');		
		}	
		public function fuzzyLists(){
			$fuzzy = db('select_cat');
			
				$arr = $fuzzy ->select();
				//转出树形图
				$s = $this ->getTrees($arr,0);
				
				return json($arr);
			
			return $this->fetch('addclassify');		
		}	

		public function goodsCatAdd(){
				$fuzzy = db('select_cat');
				$arr = $fuzzy ->where("parent_id = 0")->select();
				if(request()->isAjax()){

					if($_POST !=null){

					$arr = $fuzzy ->where("parent_id = {$_POST['id']}")->select();
					
						return json($arr);
					
				}
				
				return json($arr);
				}
		}

		public function sss(){
			echo phpinfo();
		}
		public function updateSelectCat(){
			$arr = input('post.');
			
			$id = db('select_cat') ->where('id',$arr['id']) ->update(['name' => $arr['name']] );
			if($id){
				echo 1;
			}
		}
		public function deleteCatP(){
			if(request()->isAjax()){
				$cat = db('select_cat');
				$goods = db('goods');
				$id = input('post.id');
				
				$goods -> where("stair = '{$id }' or three_level = '{$id }' or second_level = '{$id}'") ->delete();
				$cat ->where('id',$id) ->delete();
				$this -> deleteCatG($id) ;
				return json(1);

			}
		}
		//s删除 产品选择分类
		public function deleteCatG($id){
			$cat = db('select_cat');
			

			$arr = $cat ->where('parent_id',$id) -> field('id')->select();//查出子类

			if($arr){//判断是否有子类
				
			foreach ($arr as $key => $value) {

				$cat ->where('id',$value['id']) ->delete();//删除
				$this ->deleteCatG($value['id']);
				
			}
			}
		
				
			
		}

		public function interface_cat(){
					$goods = db('goods_cat');
				if(request()->isAjax()){

		            $param = input();
		          	$return = $goods -> where("parent_id = '3'") -> select();
		          	$arr ;

		          	foreach ($return as $key => $value) {
		          		$value['control'] = $return[$key]["cat_id"];
		          		$arr[$key] = $value;
		          	}

		            return json($arr);

		        }

		      return $this->fetch('interface_cat');
				}
		 public function interfaceCatAdd(){
		 	if(request()->isPost()){
			$file = request()->file("cat_pic");
			if($file){
    			// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $data = input('post.');
		        $data["cat_pic"] = $route;
		        $data['parent_id'] = 3;
		      
		        $result = db('goods_cat')->insert($data);

		        if($result){
		        	$this->success('新增成功', 'interface_cat','',2);
		        }

    		}
		}

		return $this->fetch('interfaceCatAdd');
		 }
		 public function interfaceCatUpdate(){
					 	// 更新产品大纲分类
				
					$id = input('get.id');

					$formData = db('goods_cat')->where('cat_id',$id)->select();

					$this->assign('formData',$formData);


					// 如果修改了数据
					if(request()->isPost()){
					// 获取表单上传文件 例如上传了001.jpg
			    		$file = request()->file("cat_pic");

			    		// 如果有上传图片
			    		if($file){
			    			// 移动到框架应用根目录/uploads/ 目录下
				    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

					        // 拼接完全路径
					        $route = request()->domain()."/upload/".$info->getSaveName();
					        $upData = input('post.');
					        $upData["cat_pic"] = $route;
					        	 $upData["parent_id"] =3;
					        $result = db('goods_cat')->where('cat_id',$upData["cat_id"])->update($upData);

					        if($result){
					        	$this->success('修改成功', 'interface_cat','',2);
					        }

			    		}else {

			    			$upData = input('post.');
			    			 $upData["parent_id"] =3;
			    			$result = db('goods_cat')->where('cat_id',$upData["cat_id"])->update($upData);
			    			if($result){
					        	$this->success('修改成功', 'interface_cat','',2);
					        }
			    		}
			    		
					}

					return $this->fetch('interfaceCatUpdate');

					// $conten = input('post.content');
					// $result = db('goods_cat')->where('cat_id',$requestData)->setField('cat_name',$conten);

					// if($result){
					// 	echo 1;
					// }
	
		 }
		 public function interfaceCatDatele(){

		// 接受 json 字符串
		$requestDataJson = $GLOBALS['HTTP_RAW_POST_DATA'];
		
		// json 字符串反序列化成数组
		$requestData = json_decode($requestDataJson,true);

		// 数组,可能多 id 
		$removeIds = $requestData["classifyId"];

		// 判断是否数组 , 选择右边删除为字符串 , 左边多选为数组
		$isArray = is_array($removeIds);

		if($isArray){
			// 选择多选
			foreach ($removeIds as $key => $value) {
	      		$removeProdct = db('goods')->where('cat_id',$value)->delete();
	      		$result = db('goods_cat')->where('cat_id')->delete($removeIds);
	      		// if($removeProdct){
	      		// 	$result = db('goods_cat')->where('cat_id')->delete($removeIds);
	      		// }
	      	};
		}else {
			// 选择单选 删除旗下商品
			$removeProdct = db('goods')->where('cat_id',$removeIds)->delete();
			// 再删除分类
      		$result = db('goods_cat')->where('cat_id')->delete($removeIds);
		}

		// 成功
		if($result){
			echo 1;
		}
		 }
		public function interfaces(){
			$goods = db('goods');
		 if(request()->isAjax()){
            $return = $goods -> where("goods_type = 4") -> select();
			if($return){
			 return json($return);
			}
		}else{
		// 渲染模板输出
		return $this->fetch('interface');
		}
		}
		public function interfacesAdd(){
			$fuzzy = db('select_cat');	
		$arr = $fuzzy ->select();
		$s = $this ->getTrees($arr,0);
		$list = db('goods_cat')->where('parent_id',3)->select();

		if(request()->isPost()){

			$file = request()->file("pic");
			$route = '';
			if($file){
				// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        
		        //echo db('goods')->getLastSql();
		        // var_dump($data);
		    }

		    	$data = input('post.');
		    	// if($data['stair'] ==0 or $data['second_level'] ==0 or $data['three_level'] ==0 ){
		    	// 	$this->success('请填写完产品分类', 'productAdd','',3);
		    	// }
		    	

		    	// if(!array_key_exists('content',$data)){
		    	// 	$this->success('请填写产品详情', 'productAdd','',3);
		    	// }
		    	
		        $data["pic"] = $route;
		        $data['goods_type'] = 4 ;
		        $result = db('goods_cat')->where('cat_id',$data["cat_id"])-> select();
		        $data["type"] = $result[0]["cat_name"];


		        $result = db('goods')->insert($data);  
		        if($result){
		        	$this->assign('list',$list);
		        	$this->success('新增成功', 'interfaces','',2);
		        }
			
		}else{
		$this->assign([
            'list'  => $list,
            'cat' => $s
        ]);
		

		return $this->fetch('interfacesAdd');
	}
		}




	public function interfacesCheck(){
			// 接受id 返回 商品详情
		if(request()->isGet()){
			$data = input('get.id');
			// 产品详情
			$formData = db('goods')->where('id',$data)->select();
			$cat =db('select_cat'); 
			foreach ($formData  as $key => $value) {
			           	
			           $arr	= $cat ->where('id',$value['stair']) ->find();
			           $formData[$key]['stair'] = $arr['name'];
			           $arr	= $cat ->where('id',$value['three_level']) ->find();
			           $formData[$key]['three_level'] = $arr['name'];
			           $arr	= $cat ->where('id',$value['second_level']) ->find();
			           $formData[$key]['second_level'] = $arr['name'];
			           }    

			$this->assign('formData',$formData);
			return $this->fetch('interfacesCheck');
		}else {
			$this->error('查看失败');
		}
	}

	public function interfacesChange(){
		// 下拉框
		$list = db('goods_cat')->where('parent_id',3)->select();
		$this->assign('list',$list);

		// 接受id 返回 商品详情
		if(request()->isGet()){
			$data = input('get.id');
			// 产品详情
			$formData = db('goods')->where('id',$data)->select();

			$this->assign('formData',$formData);
		}

		// 跟新数据
		if(request()->isPost()){
		
			$file = request()->file("pic");
			if($file){
				// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $data = input('post.');
		        $data["pic"] = $route;
		          $arr = db('goods_cat')->where('cat_id',$data["cat_id"]) ->find();
				$data["type"] = $arr["cat_name"] ;
		        $id = $data["id"];
		         $data['goods_type'] = 4 ;
		        $result = db('goods')->where('id',$id)->update($data);

		        if($result){
		        	$this->assign('list',$list);
		        	$this->success('更改成功', 'product','',2);
		        }	
			}else {

				$data = input('post.');
				 $arr = db('goods_cat')->where('cat_id',$data["cat_id"]) ->find();
				$data["type"] = $arr["cat_name"] ;
				$id = $data["id"];
				 $data['goods_type'] = 4 ;
				$result = db('goods')->where('id',$id)->update($data);
				if($result){
		        	$this->assign('list',$list);
		        	$this->success('更改成功', 'interfaces','',2);
		        }	
			}
		}else{	

		// // 跟新数据
		// if(request()->isPost()){
		// 	$data = input('post.');
		// 	$id = $data["id"];
		// 	$result = db('goods')->where('id',$id)->update($data);
			
		// 	if($result){

		// 		$this->assign('list',$list);
		// 		// return $this->fetch('productAdd');
		// 		$this->success('更改成功', 'product','',2);
		// 	}
		// }

		return $this->fetch('interfacesChange');}
	}

	// 更新产品大纲分类
	public function matterCatUpdatas(){
		$id = input('get.id');

		$formData = db('goods_cat')->where('cat_id',$id)->select();

		$this->assign('formData',$formData);


		// 如果修改了数据
		if(request()->isPost()){
		// 获取表单上传文件 例如上传了001.jpg
    		$file = request()->file("cat_pic");

    		// 如果有上传图片
    		if($file){
    			// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $upData = input('post.');
		        $upData["cat_pic"] = $route;
		        $upData["parent_id"] = 3;
		         $upData["goods_type"] = 4;
		        $result = db('goods_cat')->where('cat_id',$upData["cat_id"])->update($upData);

		        if($result){
		        	$this->success('修改成功', 'interface_cat','',2);
		        }

    		}else {

    			$upData = input('post.');
    			$upData["parent_id"] = 3;
    			 $upData["goods_type"] = 4;
    			$result = db('goods_cat')->where('cat_id',$upData["cat_id"])->update($upData);
    			if($result){
		        	$this->success('修改成功', 'interface_cat','',2);
		        }
    		}
    		
		}

		return $this->fetch('matterCatUpdatas');

		// $conten = input('post.content');
		// $result = db('goods_cat')->where('cat_id',$requestData)->setField('cat_name',$conten);

		// if($result){
		// 	echo 1;
		// }
	}


}